package es.ats.codington.web.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LogoutServlet
 */
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * Instantiates a new logout servlet.
     *
     * @see HttpServlet#HttpServlet()
     */
    public LogoutServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * Do get.
	 *
	 * @param request the request
	 * @param response the response
	 * @throws ServletException the servlet exception
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {                      
		// Get current session
		HttpSession session = request.getSession(true);
		                 
		//Disable Cookies
        Cookie loginCookie = new Cookie("visitor", "");        
        Cookie nameCookie = new Cookie("VisitorName", "");
   	 	Cookie lastnameCookie = new Cookie("VisitorLastName", "");
   	 	Cookie ageCookie = new Cookie("VisitorAge", "");
   	 	Cookie addressCookie = new Cookie("VisitorAddress", "");
   	 	Cookie phoneCookie = new Cookie("VisitorPhone", "");
        
        loginCookie.setMaxAge(0);  
        nameCookie.setMaxAge(0);  
        lastnameCookie.setMaxAge(0);  
        ageCookie.setMaxAge(0);  
        addressCookie.setMaxAge(0);  
        phoneCookie.setMaxAge(0);  

        response.addCookie(loginCookie);  
        response.addCookie(nameCookie); 
        response.addCookie(lastnameCookie); 
        response.addCookie(ageCookie); 
        response.addCookie(addressCookie); 
        response.addCookie(phoneCookie); 
       
        // Disable current session
        session.invalidate();  
        
        // Send user to mainpage
        request.getRequestDispatcher("login.jsp").include(request, response);  
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
